package io.github.source.bean;

import android.util.Log;

import io.github.source.Init;
import io.github.source.db.AppDatabase;

import com.github.catvod.utils.Json;
import com.google.gson.JsonElement;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;

import java.io.Serializable;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;
import io.github.source.gson.ExtAdapter;
import okhttp3.Headers;

/**
 * @ClassName: Config
 * @Description: 源站点(可以公共化)
 * 注意重写的equals，key相同时判断是相同的对象
 * @author: huangrongcai
 * @date: 2024/6/4
 * @Copyright: 黄嵘才（HuangRc)
 */
@Entity
public class Site implements Serializable {
    @NonNull
    @PrimaryKey
    @SerializedName("key")
    private String key;

    @Ignore
    @SerializedName("name")
    private String name;

    /**
     * 源站类型
     */
    @Ignore
    @SerializedName("type")
    private Integer type;
    @Ignore
    @SerializedName("api")
    private String api;
    /*1=可以搜索*/
    @SerializedName("searchable")
    private Integer searchable = 1;

    @SerializedName("changeable")
    private Integer changeable;

    @SerializedName("quickSearch")
    private Integer quickSearch;
    /**
     * 1=true
     */
    @SerializedName("filterable")
    private Integer filterable;
    @Ignore
    @JsonAdapter(ExtAdapter.class)
    @SerializedName("ext")
    private String ext = "";
    /**/
    @Ignore
    @SerializedName("jar")
    private String jar;

    @Ignore
    @SerializedName("click")
    private String click;

    /**
     * 入口播放
     */
    @Ignore
    @SerializedName("playUrl")
    private String playUrl;


    @Ignore
    @SerializedName("indexs")
    private Integer indexs;
    /**
     * 该站点无响应超时时间 秒(s)
     */
    @Ignore
    @SerializedName("timeout")
    private Integer timeout;

    /** SYS = 0;IJK = 1;EXO = 2;*/
    @Ignore
    @SerializedName("playerType")
    private Integer playerType;

    @Ignore
    @SerializedName("categories")
    private List<String> categories;

    @Ignore
    @SerializedName("header")
    private JsonElement header;

    @Ignore
    @SerializedName("style")
    private Style style;

    @Ignore
    private boolean activated;

    public static Site objectFrom(JsonElement element) {
        try {
            return Init.json().fromJson(element, Site.class);
        } catch (Exception e) {
            Log.d("TAG", "初始化json Site " + e);
            e.printStackTrace();
            return new Site();
        }
    }

    /**
     * 如果Key是Null 将返回Null
     *
     * @param key Key不可以Null
     * @return Site 或者 NULL
     */
    public static Site get(String key) {
        if (key == null) {
            return null;
        }
        Site site = new Site();
        site.setKey(key);
        return site;
    }

    public Headers getHeaders() {
        return Headers.of(Json.toMap(getHeader()));
    }

    public Site sync() {
        Site item = find(getKey());
        if (item == null) return this;
        if (getChangeable() != 0) setChangeable(Math.max(1, item.getChangeable()));
        if (getSearchable() != 0) setSearchable(Math.max(1, item.getSearchable()));
        return this;
    }

    public static Site find(String key) {
        return AppDatabase.get().getSiteDao().find(key);
    }

    public void setActivated(Site item) {
        this.activated = item.equals(this);
    }

    public boolean isEmpty() {
        return getKey().isEmpty() && getName().isEmpty();
    }

    public boolean isSearchable() {
        return getSearchable() == 1;
    }


    @NonNull
    public String getKey() {
        return key;
    }

    public void setKey(@NonNull String key) {
        this.key = key;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getApi() {
        return api;
    }

    public void setApi(String api) {
        this.api = api;
    }

    public String getExt() {
        return ext;
    }

    public void setExt(String ext) {
        this.ext = ext;
    }

    public String getJar() {
        return jar;
    }

    public void setJar(String jar) {
        this.jar = jar;
    }

    public String getClick() {
        return click;
    }

    public void setClick(String click) {
        this.click = click;
    }

    public String getPlayUrl() {
        return playUrl;
    }

    public void setPlayUrl(String playUrl) {
        this.playUrl = playUrl;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Integer getIndexs() {
        return indexs;
    }

    public void setIndexs(Integer indexs) {
        this.indexs = indexs;
    }

    public Integer getTimeout() {
        return timeout;
    }

    public void setTimeout(Integer timeout) {
        this.timeout = timeout;
    }

    public Integer getPlayerType() {
        return playerType;
    }

    public void setPlayerType(Integer playerType) {
        this.playerType = playerType;
    }

    public Integer getSearchable() {
        return searchable;
    }

    public void setSearchable(Integer searchable) {
        this.searchable = searchable;
    }

    public Integer getChangeable() {
        return changeable;
    }

    public void setChangeable(Integer changeable) {
        this.changeable = changeable;
    }

    public List<String> getCategories() {
        return categories;
    }

    public void setCategories(List<String> categories) {
        this.categories = categories;
    }

    public JsonElement getHeader() {
        return header;
    }

    public void setHeader(JsonElement header) {
        this.header = header;
    }

    public boolean isActivated() {
        return activated;
    }

    public void setActivated(boolean activated) {
        this.activated = activated;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (!(obj instanceof Site)) return false;
        Site it = (Site) obj;
        if (getKey() == null || it.getKey() == null) {
            return false;
        }
        return getKey().equals(it.getKey());
    }


    public Integer getQuickSearch() {
        return quickSearch;
    }

    public void setQuickSearch(Integer quickSearch) {
        this.quickSearch = quickSearch;
    }

    public Integer getFilterable() {
        return filterable;
    }

    public void setFilterable(Integer filterable) {
        this.filterable = filterable;
    }

    @Override
    public String toString() {
        return "Site{" +
                "key='" + key + '\'' +
                ", name='" + name + '\'' +
                ", type=" + type +
                ", api='" + api + '\'' +
                ", searchable=" + searchable +
                ", ext='" + ext + '\'' +
                ", jar='" + jar + '\'' +
                ", click='" + click + '\'' +
                ", playUrl='" + playUrl + '\'' +
                ", indexs=" + indexs +
                ", timeout=" + timeout +
                ", playerType=" + playerType +
                ", changeable=" + changeable +
                ", quickSearch=" + quickSearch +
                ", filterable=" + filterable +
                ", categories=" + categories +
                ", header=" + header +
                ", style=" + style +
                ", activated=" + activated +
                '}';
    }
}
